﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.InfoSys
{
    public partial class SysAccountWnd : UserControl
    {
        #region 成员函数
        private SysAccount _currentQueryRecord;
        private PagerIndex _pagerIndex;
        private SysAccount _selectedRecord;
        public SysAccount SelectedRecord { get { return _selectedRecord; } }
        private CommonUI.WaitForm _waitForm = new CommonUI.WaitForm();
        #endregion

        #region 构造函数
        public SysAccountWnd()
        {
            InitializeComponent();
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnNew.Image = global::Zwms.Client.Properties.Resources.add;
            this.btnEdit.Image = global::Zwms.Client.Properties.Resources.edit;
            this.btnDelete.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.query;
            this.btnResetPassword.Image = global::Zwms.Client.Properties.Resources.key;

            this.btnQry.Image = global::Zwms.Client.Properties.Resources.query;
            this.btnSel.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel2;
        }
        private void InitControl()
        {
            ResetDataPager();
            ResetQueryRecord();
            RefreshData();
        }

        public void SetQueryState()
        {
            this.btnNew.Visible = false;
            this.btnEdit.Visible = false;
            this.btnDelete.Visible = false;
            this.btnQuery.Visible = false;
            this.btnResetPassword.Visible = false;



            this.btnQry.Visible = true;
            this.btnSel.Visible = true;
            this.btnCancel.Visible = true;
        }
        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void ResetQueryRecord()
        {
            _currentQueryRecord = new SysAccount();
        }

        private void GetRecordFromUI()
        {
            _currentQueryRecord.Id = this.tbUserCode.Text.Trim();
            _currentQueryRecord.UserName = this.tbUserName.Text.Trim();
            if(!string.IsNullOrWhiteSpace(tbsUserRole.GetTextBoxContent()))
            {
                _currentQueryRecord.UserRole = tbsUserRole.Tag.ToString();
            }
            if(!string.IsNullOrWhiteSpace(tbsDepartment.GetTextBoxContent()))
            {
                _currentQueryRecord.DepartmentCode = tbsDepartment.Tag.ToString();
            }
        }

        private void RefreshData()
        {
            //读入部分数据
            _pagerIndex = new PagerIndex(dataPager.PageCurrent, CommonDefine.iPageSize);
            MyDataBind(_currentQueryRecord, _pagerIndex);
        }

        private void MyDataBind(SysAccount queryRecord, PagerIndex pIndex)
        {
            try
            {
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                IList<SysAccount> queryList = client.QuerySysAccountList(queryRecord, ref pIndex);
                dataPager.RecordCount = pIndex.RecordCount;
                LoadBillDetail(queryList);
            }
            catch
            {
            }
        }

        private void LoadBillDetail(IList<SysAccount> recordList)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                this.sysAccountBindingSource.DataSource = null;
                this.sysAccountBindingSource.DataSource = recordList;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void Add()
        {
            SysAccountNewForm window = new SysAccountNewForm();
            if (window.ShowDialog() == DialogResult.OK)
            {
                RefreshData();
            }
        }

        private void Edit()
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要编辑的项");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            SysAccount record = row.DataBoundItem as SysAccount;
            SysAccountEditForm window = new SysAccountEditForm(record.Id);
            if (window.ShowDialog() == DialogResult.OK)
            {
                RefreshData();
            }
        }

        private void ResetPassword()
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要重置密码的用户");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            SysAccount record = row.DataBoundItem as SysAccount;
            ResetPasswordForm window = new ResetPasswordForm(record.Id);
        }

        private void Delete()
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要删除的记录！");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                return;
            }

            if (MessageBox.Show("是否确认删除所选择的项？", "确认", MessageBoxButtons.OK) == DialogResult.Cancel)
            {
                return;
            }

            DataGridViewRow row = dgv.Rows[rowIndex];
            SysAccount record = row.DataBoundItem as SysAccount;
            try
            {
                //删除操作
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                client.DeleteSysAccount(record);
                //删除列表中的该行记录
                InitControl();
            }
            catch (System.Exception exc)
            {
                MessageBox.Show(this, "删除项出错，错误：" + exc.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion


        #region 控件函数
        private void tbsUserRole_SelectButtonClick(object sender, EventArgs e)
        {
            SysRoleWnd uc = new SysRoleWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择角色");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                tbsUserRole.Tag = uc.SelectedRecord.Id;
                tbsUserRole.SetTextBoxContent(uc.SelectedRecord.RoleName);
            }
        }

        private void tbsDepartment_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.DepartmentWnd uc = new InfoWms.DepartmentWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc,"选择部门");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                tbsDepartment.Tag = uc.SelectedRecord.Id;
                tbsDepartment.SetTextBoxContent(uc.SelectedRecord.DepartmentName);
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.Add();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.Edit();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            this.Delete();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.ResetDataPager();
            this.ResetQueryRecord();
            this.GetRecordFromUI();
            this.RefreshData();
        }

        private void btnResetPassword_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要编辑的项");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            SysAccount record = row.DataBoundItem as SysAccount;
            ResetPasswordForm window = new ResetPasswordForm(record.Id);
            if (window.ShowDialog() == DialogResult.OK)
            {
                this.RefreshData();
            }
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            MyDataBind(_currentQueryRecord, _pagerIndex);
        }

        private void btnQry_Click(object sender, EventArgs e)
        {
            this.ResetDataPager();
            this.ResetQueryRecord();
            this.GetRecordFromUI();
            this.RefreshData();
        }

        private void btnSel_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择列表中的记录");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            this._selectedRecord = row.DataBoundItem as SysAccount;
            if (this.Parent is Form)
            {
                Form window = this.Parent as Form;
                window.DialogResult = DialogResult.OK;
                window.Close();
            }
            else
            {
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.Parent is Form)
            {
                Form window = this.Parent as Form;
                window.DialogResult = DialogResult.Cancel;
                window.Close();
            }
            else
            {
                return;
            }
        } 	
        #endregion
    }
}
